package com.party.admin.web.controller.analysis;

import com.google.common.collect.Maps;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.admin.web.dto.output.analysis.StatisticalPageOutput;
import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.analysis.StatisticsPageCounter;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.system.TargetType;
import com.party.core.service.analysis.IStatisticsPageCountService;
import com.party.core.service.analysis.IStatisticsPageFlowService;
import com.party.core.service.member.IMemberGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * 页面分析控制器
 *
 * @author yifeng
 * @date 2018/8/8 0008
 * @time 上午 10:07
 */
@Controller
@RequestMapping("analysis/page")
public class StatisticalPageController {
    @Autowired
    private IStatisticsPageFlowService statisticsPageFlowService;
    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IStatisticsPageCountService iStatisticsPageCountService;//页面统计

//    @RequestMapping("list")
//    public ModelAndView list(StatisticsPageFlow pageFlow, CommonInput commonInput, Page page) {
//        page.setLimit(5);
//
//        CommonInput.transfromDate(commonInput);
//        Map<String, Object> params = Maps.newHashMap();
//        params.put("startDate", commonInput.getCreateStart());
//        params.put("endDate", commonInput.getCreateEnd());
//
//        if (StringUtils.isNotEmpty(commonInput.getCreateStart())) {
//            commonInput.setCreateStart(commonInput.getCreateStart().substring(0, "2018-05-04".length()));
//        }
//        if (StringUtils.isNotEmpty(commonInput.getCreateEnd())) {
//            commonInput.setCreateEnd(commonInput.getCreateEnd().substring(0, "2018-05-04".length()));
//        }
//        List<PageListOutput> statisticsPageFlows = statisticsPageFlowService.webListPage(pageFlow, params, page);
//        List<StatisticalPageOutput> pageOutputs = LangUtils.transform(statisticsPageFlows, input -> {
//            StatisticalPageOutput pageOutput = new StatisticalPageOutput();
//            try {
//                MyBeanUtils.copyBeanNotNull2Bean(input, pageOutput);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            params.put("url", input.getUrl());
//            Long uv = statisticsPageFlowService.getUv(params);
//            pageOutput.setUv(uv);
//            Long outCount = statisticsPageFlowService.getOutCount(params);
//            double div = BigDecimalUtils.div(outCount.intValue(), input.getPv());
//            pageOutput.setOutPercent((int) BigDecimalUtils.mul(div, 100));
//            Integer avgStayTime = input.getAvgStayTime().intValue();
//            if (avgStayTime > 0) {
//                pageOutput.setStayTime(DateUtils.formatTime(avgStayTime));
//            }
//            return pageOutput;
//        });
//        ModelAndView mv = new ModelAndView("analysis/pageFlowList");
//        mv.addObject("page", page);//回传参数到分页控制区
//        mv.addObject("pageFlows", pageOutputs);//页面统计结果集
//        mv.addObject("input", commonInput);//回显TAG切换标志及查询起始终止时间
//        List<Member> partnerList = memberService.getAllPartnerWithName();
//        mv.addObject("partnerList", partnerList);//回显到合作商下拉选择，建议做到缓存中
//        mv.addObject("pageFlow", pageFlow);//页面查询条件回显
//        return mv;
//    }


    @RequestMapping("list")
    public ModelAndView list(StatisticsPageCounter pageCount, CommonInput commonInput, Page page) {
        page.setLimit(10);
        if (commonInput.getTimeType() == null&&
                (StringUtils.isBlank(commonInput.getCreateEnd())||StringUtils.isBlank(commonInput.getCreateStart()))
                ){
            commonInput.setTimeType(2);
        }
        CommonInput.transfromDate(commonInput);
        Map<String, Object> params = Maps.newHashMap();
        params.put("startDate", commonInput.getCreateStart());
        params.put("endDate", commonInput.getCreateEnd());

        if (StringUtils.isNotEmpty(commonInput.getCreateStart())) {
            commonInput.setCreateStart(commonInput.getCreateStart().substring(0, 10));
        }
        if (StringUtils.isNotEmpty(commonInput.getCreateEnd())) {
            commonInput.setCreateEnd(commonInput.getCreateEnd().substring(0, 10));
        }
        List<StatisticsPageCounter> pageCountResultList =
                iStatisticsPageCountService.listPageByOthers(pageCount, params, page);
        List<StatisticalPageOutput> pageOutputs = LangUtils.transform(pageCountResultList, input -> {
            StatisticalPageOutput output = new StatisticalPageOutput();
            output.setName(input.getName());
            output.setUrl(input.getUrl());
            output.setUv(input.getUserNum());
            output.setPv(input.getPageNum());

            int outCount = input.getOutCount();
            double div = BigDecimalUtils.div(outCount, input.getPageNum());
            double mul = BigDecimalUtils.mul(div, 100);
            double round = BigDecimalUtils.round(mul, 2);
            output.setOutPercent(round);

            output.setPartnerName(input.getPartnerName());
            if (input.getStayTime() > 0) {
                double div1 = BigDecimalUtils.div(input.getStayTime(), input.getPageNum());
                output.setStayTime(DateUtils.formatTime((long) div1));
            }
            return output;
        });

        System.out.println("共查到：" + pageCountResultList.size() + "条统计结果");
        ModelAndView mv = new ModelAndView("analysis/pageCountList");
        mv.addObject("page", page);//回传参数到分页控制区
        mv.addObject("pageFlows", pageOutputs);//页面统计结果集
        mv.addObject("input", commonInput);//回显TAG切换标志及查询起始终止时间
        List<MemberGroup> partnerList = memberGroupService.getAllPartnerWithName();
        mv.addObject("partnerList", partnerList);//回显到合作商下拉选择，建议做到缓存中
        mv.addObject("pageCount", pageCount);//页面查询条件回显
        mv.addObject("modules", TargetType.values());
        return mv;
    }


}
